from errors import Duplicate, Missing
from model.user import User

_users = [
    User(name="ablojsdfo", hash="jskadhfjksdh"),
    User(name="jskfhjdsh", hash="jdsahgjdhsgd"),
]


def _find(name: str) -> User | None:
    for user in _users:
        if user.name == name:
            return user
    return None


def _check_missing(name: str):
    if not _find(name):
        raise Missing(msg=f"User '{name}' not found")


def _check_duplicate(name: str):
    if _find(name):
        raise Duplicate(msg=f"User '{name}' already exists")


def get_all() -> list[User]:
    return _users


def get_one(name: str) -> User:
    _check_missing(name)
    return _find(name)


def create(user: User) -> User:
    _check_duplicate(user.name)
    _users.append(user)
    return user


def modify(name: str, user: User) -> User:
    _check_missing(name)
    return user


def delete(name: str) -> None:
    _check_missing(name)
    return None
